<?php
/**
 * 后台管理首页控制器
 *
 */

class SiteController extends MyAdminController
{
	public function init()
	{
		parent::init();
	}

	public function actionIndex()
	{
		$data = array();
		$data['name'] = Yii::app()->user->getState('name');
		$this->render('/site/index', $data);
	}

	public function actionLogin()
	{
		if(Yii::app()->request->requestType == 'POST')
		{
			$username = trim(iconv('utf-8', 'gbk', Yii::app()->request->getParam('username')));
			$password = Yii::app()->request->getParam('password');
			$verifyCode = trim(strtolower(Yii::app()->request->getParam('verifyCode')));
			if($verifyCode == strtolower(Yii::app()->user->getState('re_code')))
			{
				Yii::app()->user->setState('re_code','');
				if($username == '' || $password == '')
				{
					$this->ajaxError('用户名或密码为能为空。');
				}

				// 使用提供的用户名和密码登录用户
				$identity = new MyUserIdentity($username, $password);
				
				if($identity->admin_authenticate())
				{
					$this->ajaxError(Yii::app()->user->login($identity,3600), 0);
				}
				else
				{
					$this->ajaxError('用户名或密码错误。');
				}
			}
			else
			{
				Yii::app()->user->setState('re_code','');
				$this->ajaxError('验证码错误。');
			}
		}
		else
		{
			//已经登陆跳转到首页
			if(!Yii::app()->user->isGuest && Yii::app()->user->getState('admin_is_login'))
			{
				Yii::app()->request->redirect('/site/index');
			}
			else
			{
				//显示登陆界面
				$this->render('login');
			}
		}
	}
}